package q2016_03_02.leader.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import q2016_03_02.leader.bean.User;

public class UserDao {
	private List<User> userList = new CopyOnWriteArrayList<>();
	
	private String filename;
	
	public UserDao(String filename) {
		super();
		this.filename = filename;
	}

	public void init(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		File file = new File(filename);
		String charset = "UTF-8";
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			if(!file.exists()){
				file.createNewFile();
			}
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, charset);
			br = new BufferedReader(isr);
			String str;
			while( (str = br.readLine()) != null ){
				String[] strArray = str.split(",");
				User user = new User();
				user.setUserId(Long.parseLong(strArray[0]));
				user.setUserName(strArray[1]);
				user.setPassword(strArray[2]);
				user.setEnabled(Integer.parseInt(strArray[3])>0);
				user.setRegDate(sdf.parse(strArray[4]));
				userList.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(br != null){
					br.close();
				}
				if(isr != null){
					isr.close();
				}
				if(fis != null){
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void destroy(){
		File file = new File(filename);
		String charset = "UTF-8";
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try{
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos, charset);
			for(User user : userList){
				osw.write(user.toString()+"\r\n");
			}
			osw.flush();
		}catch (Exception e) {
				e.printStackTrace();
		}finally{
			try {
				if(osw != null){
					osw.close();
				}
				if(fos != null){
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public boolean createUser(User user)
	{
		for(User u : userList){
			if(u.getUserId() == user.getUserId()){
				return false;
			}
		}
		return userList.add(user);
	}
	
	public boolean deleteUser(long userId)
	{
		for(User user : userList){
			if(user.getUserId() == userId){
				return userList.remove(user);
			}
		}
		return false;
	}
	
	public boolean disableUser(long userId)
	{
		for(User user : userList){
			if(user.getUserId() == userId){
				userList.remove(user);
				user.setEnabled(false);
				userList.add(user);
				return true;
			}
		}
		return false;
	}
	
	public List<User> queryUsers(String userNamePrex,boolean onlyValidUser)
	{
		List<User> list = new ArrayList<User>();
		for(User user : userList){
			if(onlyValidUser){
				if(user.getUserName().matches(userNamePrex) &&
						user.isEnabled() ){
					list.add(user);
				}
			}else{
				if(user.getUserName().matches(userNamePrex)){
					list.add(user);
				}
			}
		}
		return list;
	}
}
